package com.zzl.web;

import com.alibaba.fastjson.JSONObject;
import com.zzl.entity.Grade;
import com.zzl.services.GradeServices;
import com.zzl.services.impl.GradeServicesImpl;
import com.zzl.util.DbUtil;
import com.zzl.util.ResponseTypeUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

public class GradeDeleteServlet extends HttpServlet {
    DbUtil dbUtil=new DbUtil();
    GradeServices gradeServices=new GradeServicesImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String delIds=req.getParameter("delIds");
//        StringBuilder validDelIds = new StringBuilder();
        Connection con = null;
        if (delIds != null && !delIds.isEmpty()) {
            try {
                // 获取数据库连接
                con = dbUtil.getCon();
                JSONObject result = new JSONObject();
                // 将获取到的 delIds 字符串按逗号分隔成字符串数组
                String[] idArray = delIds.split(",");
                for (int i = 0; i < idArray.length; i++) {
                    boolean hasStudents = gradeServices.hasStudentsInGrade(con, idArray[i]);

                    if (hasStudents) {
                        result.put("errorIndex", i);
                        result.put("errorMsg", "删除失败，该班级下有学生");
                        ResponseTypeUtil.write(resp, result);
                        return;
                    }
                }
                int delNums = gradeServices.gradeDelete(con, delIds);
                if (delNums > 0) {
                    result.put("success", "true");
                    result.put("delNums", delNums);
                } else {
                    result.put("errorMsg", "删除失败");
                }

            } catch (Exception e) {
                // 处理获取数据库连接或其他操作时可能出现的异常
                e.printStackTrace();
            } finally {
                try {
                    // 关闭数据库连接
                    if (con != null) {
                        dbUtil.closeCon(con);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            con=dbUtil.getCon();
            JSONObject result=new JSONObject();
            String str[]=delIds.split(",");
            int delNums= gradeServices.gradeDelete(con,delIds);
            if(delNums>0){
                result.put("success","true");
                result.put("delNums",delNums);
            }else {
                result.put("errorMsg","删除失败");
            }
            ResponseTypeUtil.write(resp,result);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
